﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using IHISCETeim.Library;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using IHISCETeim.Model;

namespace IHISCETeim.DAL
{
    public class PartsDebtDAL
    {
        public static int Add(PartsDebtInfo obj)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("PartsDebt_Add");
            DB.AddInParameter(dbCommand, "State", DbType.Int32, obj.State);
            DB.AddInParameter(dbCommand, "SaleDate", DbType.DateTime, obj.SaleDate);
            DB.AddInParameter(dbCommand, "CardCode", DbType.String, obj.CardCode);
            DB.AddInParameter(dbCommand, "SalesAmount", DbType.Decimal, obj.SalesAmount);
            DB.AddInParameter(dbCommand, "ORDRNum", DbType.String, obj.ORDRNum);
            DB.AddInParameter(dbCommand, "ODLNNum", DbType.String, obj.ODLNNum);
            DB.AddInParameter(dbCommand, "OINVNum", DbType.String, obj.OINVNum);
            DB.AddInParameter(dbCommand, "Comments", DbType.String, obj.Comments);
            DB.AddInParameter(dbCommand, "OldDebtId", DbType.Int32, obj.OldDebtId);

            //return Convert.ToInt32(DB.ExecuteScalar(dbCommand));
            return Convert.ToInt32(DB.ExecuteScalar(dbCommand));
        }
        public static bool Delete(int DebtId)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("PartsDebt_Delete", DebtId);
            try
            {
                int r=Convert.ToInt32(  DB.ExecuteScalar(dbCommand));
                return r>0;
            }
            catch
            {
                return false;
            }
        }
        public static IDataReader GetById(int DebtId)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("PartsDebt_GetById", DebtId);
            return DB.ExecuteReader(dbCommand);
        }
        public static DataTableCollection GetPagination(string whereSql, int PageIndex, int PageSize, string Sort, ref int TotalCount)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("PartsDebt_GetPagination");
            DB.AddInParameter(dbCommand, "whereSql", DbType.String, whereSql);
            DB.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            DB.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            DB.AddInParameter(dbCommand, "Sort", DbType.String, Sort);
            DB.AddOutParameter(dbCommand, "TotalCount", DbType.Int32, 8);
            DataTableCollection dt = DB.ExecuteDataSet(dbCommand).Tables;

            TotalCount = Convert.ToInt32(DB.GetParameterValue(dbCommand, "TotalCount"));
            return dt;
        }


        #region
        public static DataTableCollection PartsDebtReport1(string SaleDateFrom, string SaleDateTo, string YuQiDate)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("PartsDebt_Report1");
            DB.AddInParameter(dbCommand, "SaleDateFrom", DbType.String, SaleDateFrom);
            DB.AddInParameter(dbCommand, "SaleDateTo", DbType.String, SaleDateTo);
            DB.AddInParameter(dbCommand, "YuQiDate", DbType.String, YuQiDate);
            DataTableCollection dt = DB.ExecuteDataSet(dbCommand).Tables;
                       
            return dt;
        }
        #endregion
    }
}
